package com.obrock;

import android.app.DialogFragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 02.11.12
 * Time: 11:06
 * To change this template use File | Settings | File Templates.
 */
public class EditComplex extends DialogFragment {

    EditText name;
    Spinner to;
    EditText amount;
    Button save;
    Button delete;
    Button cancel;
    TextView balanceTo;

    Database db;
    ArrayAdapter<Object> adapter = null;

    Balance balance = null;
    Complex complex = null;

    Balance prognosis = null;
    boolean created = false;
    double baseAmount = 0.0;

    public interface EditComplexListener {
        boolean onUpdateComplex(Complex complex);
        boolean onAppendComplex(Complex complex);
        boolean onDeleteComplex(Complex complex);
    }

    public EditComplex(Database db, Balance balance, Complex complex, ArrayAdapter<Object> adapter) {
        this.db = db;
        this.adapter = adapter;
        this.balance = balance;
        if (complex == null) {
            this.complex = new Complex(db, balance);
            this.complex.amount = balance.recalcComplex();
            created = true;
        } else {
            this.complex = complex;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edit_complex, container);

        name = (EditText) view.findViewById(R.id.name);
        name.setText(complex.name);

        balanceTo = (TextView) view.findViewById(R.id.balance_to);

        /*List<Account> accounts = new ArrayList<Account>(0);
        accounts.add(new Account(Main.db, getResources().getString(R.string.transfer_empty)));
        accounts.addAll(Arrays.asList(Main.accounts));
        ArrayAdapter<Account> dataAdapter = new ArrayAdapter<Account>(Main.context, android.R.layout.simple_spinner_item, accounts);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        */

        to = (Spinner) view.findViewById(R.id.to);
        to.setAdapter(adapter);
        to.setSelection(Main.getAccountIndex(complex.account) + 1);
        to.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> a, View v, int index, long pos) {
                //Account ac = (Account) a.getAdapter().getItem(index);
                if (a.getAdapter().getItem(index) instanceof Account) {
                    complex.referenceID = -1;
                    complex.account = (Account) a.getAdapter().getItem(index);
                } else {
                    Balance bal = (Balance) a.getAdapter().getItem(index);
                    if (bal != null) {
                        complex.referenceID = bal.parentID;
                        complex.account = bal.accountTo;
                        if (baseAmount == 0 || bal.amount <= baseAmount) {
                            amount.setText(String.format("%.2f", bal.amount));
                        } else {
                            amount.setText(String.format("%.2f", baseAmount));
                        }
                        name.setText(bal.name);
                    }
                }

                double currentAmount = 0.0;
                if (complex.account != null) {
                    if (complex.account.accountID == -1) {
                        complex.account = null;
                    } else {
                        currentAmount = complex.account.balance;
                    }
                }

                balanceTo.setText(String.format("%.2f", currentAmount));
                balanceTo.setTextColor(Main.getAccountColor(complex.account));
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                //account.type = Account.TYPE_NORMAL;
            }
        });

        baseAmount = complex.amount;
        amount = (EditText) view.findViewById(R.id.amount);
        amount.setText(String.format("%.2f", complex.amount));
        amount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    if (Main.eval(amount.getText().toString(), 0.0) == 0) {
                        amount.setText("");
                    }
                }
            }
        });
        amount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().contains("=") || charSequence.toString().contains("\n")) {
                    amount.setText(String.format("%.2f", Main.eval(charSequence.toString(), balance.amount)));
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });


        save = (Button) view.findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                complex.amount = Main.eval(amount.getText().toString(), balance.amount);
                complex.name = name.getText().toString();
                EditComplexListener activity = (EditComplexListener) getActivity();
                boolean result = false;
                if (created) {
                    result = activity.onAppendComplex(complex);
                } else {
                    result = activity.onUpdateComplex(complex);
                }
                if (result) {
                    EditComplex.this.dismiss();
                } else {
                    Main.toast(getResources().getString(R.string.oversum));
                }
            }
        });

        delete = (Button) view.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditComplexListener activity = (EditComplexListener) getActivity();
                if (activity.onDeleteComplex(complex)) {
                    EditComplex.this.dismiss();
                }
            }
        });

        cancel = (Button) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditComplex.this.dismiss();
            }
        });


        getDialog().setTitle(getResources().getString(R.string.transfer_complex));

        return view;
    }
}
